﻿using System;
using Microsoft.Research.Kinect.Nui;
using System.Collections;
using System.Threading;

namespace KiMoCSys.core
{
    /// <summary>
    /// This class detects how many kinect devices are plugged in the computer
    /// and creates the number of KinectDevice instances as necessary.
    /// 
    /// Only this class can create or destroy kinect devices on memory.
    /// For access data read from a kinect device, use FrameSkeletonManager.
    /// </summary>
    class KinectDeviceManager
    {
        private static KinectDeviceManager singleton;
        ArrayList devices;

        public static KinectDeviceManager getSingleton()
        {
            if (singleton == null )
                singleton = new KinectDeviceManager();
            return singleton;
        }

        public int Count { get { return devices.Count; } }

        /// <summary>
        /// It find all kinect devices connected in the current PC system
        /// and create the KinectDevice instance
        /// </summary>
        /// <returns></returns>
        public bool init()
        {
            if (singleton == null)
            {
                Console.WriteLine("KinectDeviceManager - KinectDeviceManager has not been initialized!");
                return false;
            }
            if (Runtime.Kinects.Count < 1)
            {
                Console.WriteLine("KinectDeviceManager - No connected kinect devices has been found in this system!");
                return false; 
            }

            devices = new ArrayList();
            KinectDevice aux;
            int index = 0;
            foreach (Runtime kinect in Runtime.Kinects)
            {
                aux = new KinectDevice(kinect, RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor,index);
                devices.Add(aux);
                index++;
            }
            //Set a handler for receiving notifications on kinect connected,disconnected and so on.
            Runtime.Kinects.StatusChanged += new EventHandler<StatusChangedEventArgs>(onKinectStatusChange);
            return true;
        }

        private void onKinectStatusChange(object sender, StatusChangedEventArgs e)
        {
            Console.WriteLine("KinectDeviceManager::onKinectStatusChange - " +e.Status.ToString());
        }


        public static void notifyNewFrame(KinectDevice device, FrameSkeleton skeleton)
        {
            if (singleton == null)
            {
                Console.WriteLine("KinectDeviceManager::notifyNewFrame - KinectDeviceManager has not been initialized");
                return;
            }

            if (device == null)
            {
                Console.WriteLine("KinectDeviceManager::notifyNewFrame - Device must not be null");
                return;
            }
            //SCLG Now we have to extract the info and store each frame of the skeleton read.
            //
            
            FrameSkeletonManager.getSingleton().addSkeletonFrame(skeleton, device.DeviceIndex);
        }

        
    }
}
